package com.zqh.reentrantLock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 曾庆红
 * {@code @date} 2023-10-14 10:13
 * 公平锁测试
 */

public class T3 implements Runnable{
    //创建公平锁
    private static ReentrantLock lock = new ReentrantLock(true);
    @Override
    public void run() {
        for(int i = 0; i<100; i++)
        {
            lock.lock();
            try{
                System.out.println(Thread.currentThread().getName()+ "获得锁");
            }finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        T3 t =new T3();
        Thread t1 = new Thread(t);
        Thread t2 = new Thread(t);
        t1.start();
        t2.start();
    }
}
